package com.baomidou.springwind.excel;

import com.baomidou.springwind.entity.Goods;
import com.baomidou.springwind.entity.ReceiveGoods;
import com.baomidou.springwind.service.GoodsService;
import com.baomidou.springwind.service.ReceiveGoodsService;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * excel文件读取
 *
 * @author luox
 */
@Component
public class MaterielDataReader extends ExcelReader<Goods> {

    @Autowired
    private GoodsService goodsService;

    @Override
    protected String saveData(List<Goods> dataList, Long batchId) {
        for (Goods goods : dataList) {
            goods.setBatchId(batchId);
            goods.setCreateTime(new Date());
            goods.setOperatorId(getCurrentUserId().intValue());
        }
        goodsService.insertBatch(dataList, 100);
        return "";
    }

    @Override
    protected List<Goods> verifyData(HSSFWorkbook workbook) {
        List<Goods> goodsList = new ArrayList<Goods>();
        HSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        Goods goods;
        for (int i = 1; i <= lastRowNum; i++) {
            sheet.getRow(i);
            goods = new Goods();
            goods.setCode(getStringCellValue(sheet.getRow(i), 1));
            goods.setName(getStringCellValue(sheet.getRow(i), 2));
            goods.setPrice(new BigDecimal(getStringCellValue(sheet.getRow(i), 3)));
            goods.setVolume(new BigDecimal(getStringCellValue(sheet.getRow(i), 4)));
            goods.setTeu(getIntCellValue(sheet.getRow(i), 5));
            goods.setStandardStock(getIntCellValue(sheet.getRow(i), 6));
            goods.setInitialStock(getIntCellValue(sheet.getRow(i), 7));
            goods.setUnitPallet(getIntCellValue(sheet.getRow(i), 8));
            goodsList.add(goods);
        }
        return goodsList;
    }

    @Override
    protected List<Goods> verifyData7(XSSFWorkbook workbook) {
        return null;
    }


}
